package com.iweb.servlet;
import com.iweb.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet(name = "LoginServlet04",urlPatterns = {"/servlet/LoginServlet04","/servlet/LoginServlet04.do"})
public class LoginServlet04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String path = req.getContextPath();
        //验证
        if ("admin".equals(username)&&"123456".equals(password)) {
            //验证通过
            User user = new User();
            user.setEmail("111@qq.com");
            user.setUsername("admin");
            user.setUserId("1");
            List<String> habits = new ArrayList<>();
            habits.add("sing");
            habits.add("dance");
            habits.add("rap");
            habits.add("basketball");
            Map<String, String> myMap = new HashMap<>();
            myMap.put("one", "tom");
            myMap.put("two", "kite");
            myMap.put("three", "jerry");
            req.setAttribute("myMap",myMap);
            req.setAttribute("loginUser",user);
            req.getRequestDispatcher("/day04/admin.jsp").forward(req,resp);
        }else{
            resp.sendRedirect(path+"/day04/login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        this.doGet(req, resp);
    }
}
